var express = require('express');
var router = express.Router();
var db = require("../model/db");
router.get("/all", async function (req, res) {
    let page = parseInt(req.query.page);
    let limit = parseInt(req.query.limit);
    if (!page) {
        page = 1;
    }
    if (!limit) {
        limit = 3
    }
    if (!req.query.field) {
        req.query.field = "_id";//默认按照_id排序
    }
    if (!req.query.sort) {
        req.query.sort = "asc";//默认按照升序排序
    }
    //拼接排序字符串
    let s = req.query.field;
    if (req.query.sort === "desc") {
        s = "-" + s;
    }
    let condition = {};
    if (req.query.key) {
        const exp = new RegExp(req.query.key, "i")//不区分大小写，模糊查询正则表达式
        condition = {
            $or: [
                { name: { $regex: exp } },
                { businessphone: { $regex: exp } },
                { address: { $regex: exp } }
            ]
        }
    }
    const data = await db.Stores.find(condition).sort(s).skip((page - 1) * limit).limit(limit);
console.log(data)
    
    res.send({
        code: 0,
        msg: "",
        count: await db.Stores.countDocuments(condition),
        data
    });
})
router.get("/:id", async function (req, res) {
    const data = await db.Stores.findById(req.params.id)
    res.send({
        code: 0,
        msg: "",
        data
    });
})
router.post("/add", async function (req, res) {
    const s = await db.Stores.findOne({
        businessphone: req.body.businessphone
    })
    if (s) {
        res.send({
            code: 0,
            msg: "",
            data: false
        });
        return;
    }
    await db.Stores.create(req.body);
    res.send({
        code: 0,
        msg: "",
        data: true
    });
})
//添加员工  （徐先生）
router.post("/emp/:id", async function (req, res) {
    const data = await db.Stores.findById(req.params.id)
    data.employees.push(req.body)
    await db.Stores.updateOne({
        _id: req.params.id
    }, {
        "employees":data.employees
    })
    const resp = await db.Stores.findById(req.params.id)

    res.send({
        code: 0,
        msg: "",
        data: resp
    });
})
//删除员工  （徐先生）
router.put("/emp/:id", async function (req, res) {
    const data = await db.Stores.findById(req.params.id)
    
     for(let i=0;i<data.employees.length;i++){
         if(data.employees[i]._id==req.body.id){
            data.employees.splice(i,1)  
         }  
     }
     await db.Stores.updateOne({
        _id: req.params.id
    }, {
        "employees":data.employees
    })
    const resp = await db.Stores.findById(req.params.id)

    res.send({
        code: 0,
        msg: "",
        data: resp
    });
})


//添加service  （徐先生）
router.post("/service/:id", async function (req, res) {
    const data = await db.Stores.findById(req.params.id)
    data.service.push(req.body)
    await db.Stores.updateOne({
        _id: req.params.id
    }, {
        "service":data.service
    })
    const resp = await db.Stores.findById(req.params.id)
    res.send({
        code: 0,
        msg: "",
        data: resp
    });
})
//删除service   （徐先生）
router.put("/service/:id", async function (req, res) {
    const data = await db.Stores.findById(req.params.id)
    
     for(let i=0;i<data.service.length;i++){
         if(data.service[i]._id==req.body.id){
            data.service.splice(i,1)  
         }  
     }
     await db.Stores.updateOne({
        _id: req.params.id
    }, {
        "service":data.service
    })
    const resp = await db.Stores.findById(req.params.id)

    res.send({
        code: 0,
        msg: "",
        data: resp
    });
})


//添加good  （徐先生）
router.post("/good/:id", async function (req, res) {
    const data = await db.Stores.findById(req.params.id)
    data.good.push(req.body)
    await db.Stores.updateOne({
        _id: req.params.id
    }, {
        "good":data.good
    })
    const resp = await db.Stores.findById(req.params.id)

    res.send({
        code: 0,
        msg: "",
        data: resp
    });
})
//删除good （徐先生）
router.put("/good/:id", async function (req, res) {
    const data = await db.Stores.findById(req.params.id)
    
     for(let i=0;i<data.good.length;i++){
         console.log(data.good[i]._id)
         if(data.good[i]._id==req.body.id){
            data.good.splice(i,1)  
         }  
     }
     await db.Stores.updateOne({
        _id: req.params.id
    }, {
        "good":data.good
    })
    const resp = await db.Stores.findById(req.params.id)

    res.send({
        code: 0,
        msg: "",
        data: resp
    });
})
router.put("/:id", async function (req, res) {
    await db.Stores.updateOne({
        _id: req.params.id
    }, req.body)
    res.send({
        code: 0,
        msg: "",
        data: true
    })
})
router.delete("/:id", async function (req, res) {
    await db.Stores.deleteOne({
        _id: req.params.id
    })
    res.send({
        code: 0,
        msg: "",
        data: true
    })
})


//修改state状态  （徐先生）
router.put("/state/:id", async function (req, res) {
    const data = await db.Stores.findById(req.params.id)
    data.state=req.body.state
    await db.Stores.updateOne({
        _id: req.params.id
    }, {
        "state":data.state
    })
    const resp = await db.Stores.findById(req.params.id)
    res.send({
        code: 0,
        msg: "",
        data: resp
    });
})
module.exports = router;